package com.ling.service;

import com.ling.bean.Answer;
import com.ling.bean.AnswerExample;
import com.ling.dao.AnswerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerService {

    private final AnswerMapper answerMapper;

    @Autowired
    public AnswerService(AnswerMapper answerMapper) {
        this.answerMapper = answerMapper;
    }

    //查询指定问题下的所有回答，按照置顶逆序，点赞逆序，时间逆序
    public List<Answer> getAnswerListByQuestionId(String questionId) {
        AnswerExample answerExample = new AnswerExample();
        answerExample.setOrderByClause("top DESC , upvoteNum DESC , editTime DESC");
        answerExample.createCriteria().andQuestionidEqualTo(questionId);
        return answerMapper.selectByExampleWithUserAndBlobs(answerExample);
    }

    //保存回答
    public void saveAnswer(Answer answer) {
        answerMapper.insertSelective(answer);
    }

    //获取指定id 的回答
    public Answer getAnswerById(String answerId) {
        return answerMapper.selectByPrimaryKey(answerId);
    }

    //修改记录
    public void updateAnswer(Answer answer) {
        answerMapper.updateByPrimaryKeySelective(answer);
    }

    //删除记录
    public void deleteAnswer(String answerId) {
        answerMapper.deleteByPrimaryKey(answerId);
    }


    //获取指定用户的所有回答,按照添加回答时间逆序
    public List<Answer> getAllUserAnswer(String userId) {
        return answerMapper.selectByExampleWithBLOBsAndQuestion(userId);
    }

    //查询所有的回答
    public List<Answer> getAllAnswer() {
        AnswerExample answerExample = new AnswerExample();
        return answerMapper.selectByExampleWithBLOBs(answerExample);
    }
}
